<template>
  <commonEmpty v-if="!riskList.length" :text="`暂无风险信息`" />
  <template v-else>
    <div v-ai-tip class="flex-auto flex flex-col">
      <div class="container flex-auto">
        <riskInfoCard v-for="item of riskList" :key="item" :info="item" />
      </div>
    </div>
  </template>
</template>

<script setup>
import { computed, onMounted, defineProps } from "vue";
import riskInfoCard from "./riskInfoCard.vue";
import { useCustomerStore, useBusinessStore } from "@/store";
import commonEmpty from "../commonEmpty.vue";

const store = useCustomerStore();
const businessStore = useBusinessStore();
const props = defineProps({
  isCustomer: {
    type: Boolean,
    default: true,
  },
});

onMounted(() => getData());

function getData() {
  if (props.isCustomer) {
    store.getCustomerRiskAction();
  } else {
    businessStore.getBusinessRiskAction();
  }
}

const cp_riskInfo = computed(() => {
  if (props.isCustomer) {
    return store.riskInfo;
  } else {
    return businessStore.riskInfo;
  }
});

function makeInfo(title, des, tip, btnText) {
  return {
    title,
    des,
    tip,
    btnText,
  };
}

const riskList = computed(() => {
  const riskInfo = cp_riskInfo.value;
  const {
    contactInsufficient, // 是否 参会人不足
    decisionContactInsufficient, // 是否 决策人数不足
    frequencyMentionCompetitor, // 是否 竞争对手提及次数过高
    frequencyMentionSameCompetitor, //是否 频繁提及同一竞争对手
    highRiskInVisit, //是否 沟通过程高危风险过多
    silenceRisk, // 沉默风险
    silenceDays, // 距离天数
    negativeAttitude, //是否 客户态度持续消极
    salesPerformanceIssue, //是否 销售表现持续不符合预期
    contactCount, //参会人数
    decisionContactCount, //决策人数
    sameCompetitorName, //同一竞争对手名称
    highRiskCount, //高风险项数
    negativeAttitudeCount, //客户态度持续消极次数,
    salesPerformanceIssueCount, //销售表现持续不符合预期连续次数
    mentionedCompetitorName, // 提及名称
    taskCompleteIssue, // 销售任务达成持续不达标
    customerSuccessIssue, // 客户推进受阻
    config,
  } = riskInfo;

  const {
    companyVisitInterval,
    taskRate4Pass,
    abilityScore4Pass,
    customerSuccessCheckThreshold,
    companyContactCount,
    taskCheckThreshold, // 销售任务达成率得分连续次数
  } = config || {};

  let array = [];

  // 客户情绪
  if (negativeAttitude) {
    const title = `客户情绪持续消极`;
    const des = `客户整体情绪已连续${negativeAttitudeCount}次评为消极`;
    const tip = `回顾历史沟通记录，识别原因并改进`;
    const btnText = `查看沟通记录`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 客户参会人数量不足
  if (contactInsufficient) {
    const title = `客户参会人数量不足`;
    const des = `当前参会人个数为：${contactCount}`;
    const tip = `为确保销售覆盖面，建议添加至少${companyContactCount}位相关联系人`;
    const btnText = `查看参会人`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  //距离上一次沟通已有：15天
  // 沉默风险
  if (silenceRisk) {
    const title = `客户沉默风险`;
    const des = `距离上一次沟通已有：${silenceDays}天，超过${companyVisitInterval}天`;
    const tip = `建议至少每周与客户保持一次有效沟通`;
    const btnText = `查看沟通记录`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 销售表现持续不符合预期
  if (salesPerformanceIssue) {
    const title = `销售能力持续不达标`;
    const des = `销售能力评估得分连续 ${salesPerformanceIssueCount} 次低于${abilityScore4Pass}分`;
    const tip = `建议查看沟通辅导页面中能力弱项，通过「去提升」入口学习能力提升方案，加强自身能力培养`;
    const btnText = `查看沟通记录`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 竞争对手提及次数过多
  if (frequencyMentionCompetitor) {
    const title = `竞争对手提及次数过多`;
    const des = `客户在上一次沟通中提及${mentionedCompetitorName}`; // 次数
    const tip = `识别客户对竞争对手的关注点，并针对性地提供自身产品的独特卖点`;
    const btnText = `查看竞争对手`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 沟通过程风险项较多
  if (highRiskInVisit) {
    const title = `沟通过程风险项较多`;
    const des = `最近一次沟通中有高危风险：${highRiskCount}项`;
    const tip = `回顾历史沟通记录，查看风险项与应对策略`;
    const btnText = `查看沟通记录`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 频繁提及同一竞争对手
  if (frequencyMentionSameCompetitor) {
    const title = `频繁提及同一竞争对手`;
    const des = `客户在最近3次会议中提及竞争对手：${sameCompetitorName}`; //次数
    const tip = `建议关注竞争对手的动向，并调整销售策略`;
    const btnText = `查看竞争对手`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 缺乏高级别客户参会人
  if (decisionContactInsufficient) {
    const title = `缺乏高级别客户参会人`;
    const des = `当前客户参会人中决策人（包含最终决策与建议决策）个数为：${decisionContactCount}`;
    const tip = `建议在后续沟通中邀请客户决策人或具有决策影响力的关键人员参与`;
    const btnText = `查看沟通记录`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 销售任务达成持续不达标
  if (taskCompleteIssue) {
    const title = `销售任务达成持续不达标`;
    const des = `销售任务达成率得分连续 ${taskCheckThreshold} 次低于${taskRate4Pass}%`;
    const tip = `建议查看沟通辅导页面中未达标的任务明细，在后续沟通中注意关键任务执行`;
    const btnText = `查看沟通记录`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  // 客户推进受阻
  if (customerSuccessIssue) {
    const title = `客户推进受阻`;
    const des = `沟通目标连续${customerSuccessCheckThreshold}次未达成`;
    const tip = `建议优化沟通计划，提升沟通策略，增加客户互动，提高沟通达成率`;
    const btnText = `查看沟通记录`;
    array.push(makeInfo(title, des, tip, btnText));
  }

  return array;
});
</script>

<style lang="less" scoped></style>
